package models

import (
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"gorm.io/gorm/schema"
	"log"
)

var db *gorm.DB

func init() {
	var err error
	dsn := "root:root@tcp(127.0.0.1:3306)/ping?charset=utf8mb4&parseTime=True&loc=Local"
	db, err = gorm.Open(mysql.Open(dsn), &gorm.Config{
		NamingStrategy: schema.NamingStrategy{
			SingularTable: true, //todo: 表明复数禁用
		},
	})
	if err != nil {
		log.Println("数据库连接失败", err)
		return
	}
	err = db.AutoMigrate(new(Users), new(Files))
	if err != nil {
		log.Println("建表失败")
		return
	}
}

// Users todo: 用户表
type Users struct {
	gorm.Model
	Username string `gorm:"type:varchar(14);not null;comment:账号"`
	Password string `gorm:"type:varchar(255);not null;comment:密码"`
	Vip      int    `gorm:"type:int(11);not null;comment:vip等级"`
	Capacity int    `gorm:"type:int(255);not null;comment:容量"`
	Status   int    `gorm:"type:tinyint;not null;comment:账号状态"`
}

// Files todo: 文件信息表
type Files struct {
	gorm.Model
	Userid   int    `gorm:"type:int(11);not null;comment:账号ID"`
	FileName string `gorm:"type:varchar(255);not null;uniqueIndex;comment:文件名"`
	Src      string `gorm:"type:varchar(255);not null;comment:文件地址;"`
	SubData  string `gorm:"type:varchar(255);not null;comment:提交信息;"`
	KData    string `gorm:"type:varchar(255);not null;comment:扩展信息;"`
	Name     string `gorm:"type:varchar(255);not null;comment:署名;"`
	TypeName string `gorm:"type:varchar(255);not null;comment:文件类型;"`
	GitSrc   string `gorm:"type:varchar(255);not null;comment:文件目标分支;"`
	Size     int    `gorm:"type:int(255);not null;comment:文件大小"`
}
